#include <iostream>
#include <stdio.h>
#include <vector>
#include <stdlib.h>

using namespace std;
struct nmp
{
	int a; 
	int b; 
	int c;
	int d;
	nmp(int a1, int b1, int c1) {
		a = a1; b = b1; c = c1, d = 0;
	};
};

void FindMinD(int S1[], int n1, int S2[], int n2, int S3[], int n3) {
	vector<nmp> vec;
	int minD = INT_MAX;
	for (int i = 0; i < n1; ++i) {
		for (int j = 0; j < n2; ++j) {
			for (int k = 0; k < n3; ++k) {
				int d = abs(S1[i] - S2[j]) + abs(S2[j] - S3[k]) + abs(S3[k] - S1[i]);
				if (d <= minD) {
					nmp pes(S1[i], S2[j], S3[k]);
					pes.d = d;
					vec.push_back(pes);
					minD = d;
				}
			}
		}
	}

	cout << "d=" << minD << " ";
	for (int i = 0; i < vec.size(); ++i) {
		if (vec[i].d == minD)
			cout << "-->" << vec[i].a << " " << vec[i].b << " " << vec[i].c << " " << vec[i].d;
	}
}

//优化时间复杂度
int is_min_data(int a, int b, int c) {
	if (a <= b && a <= c)
		return 1;
	return 0;
}
void FindMinD1(int S1[], int n1, int S2[], int n2, int S3[], int n3) {
	int i = 0, j = 0, k = 0;
	int a, b, c;
	int minD = INT_MAX;
	while (i < n1 && j < n2 && k < n3) {
		int d = abs(S1[i] - S2[j]) + abs(S2[j] - S3[k]) + abs(S3[k] - S1[i]);
		if (d < minD) {
			minD = d;
			a = S1[i];
			b = S2[j];
			c = S3[k];
		}

		if (is_min_data(S1[i], S2[j], S3[k]))
			i++;
		else if (is_min_data(S2[j], S3[k], S1[i]))
			j++;
		else 
			k++;
	}

	cout << "d=" << minD << " -->" << a << " " << b << " " <<  c;
}

int main() {
	int S1[] = {-1, 0, 9};
	int S2[] = {-25, -10, 10 ,11};
	int S3[] = {2, 9, 17, 30, 41};
	int n1 = sizeof(S1) / sizeof(S1[0]), n2 = sizeof(S2) / sizeof(S2[0]), n3 = sizeof(S3) / sizeof(S3[0]);

	FindMinD(S1, n1, S2, n2, S3, n3);
	cout << endl;
	FindMinD1(S1, n1, S2, n2, S3, n3);
	return 0;
	
}